package cn.seaboot.admin.user.web;

import cn.seaboot.admin.excel.manager.ExcelTemplateService;
import cn.seaboot.admin.mvc.Pagination;
import cn.seaboot.admin.mvc.Result;
import cn.seaboot.admin.user.bean.User;
import cn.seaboot.admin.user.service.UserService;
import cn.seaboot.commons.core.Converter;
import cn.seaboot.commons.file.ResponseWriter;
import cn.seaboot.excel.ExcelBook;
import cn.seaboot.excel.call.ExcelCallback;
import com.github.pagehelper.PageInfo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 强制：任何返回前端的用户信息，密码必须置空
 *
 * @author ChenSS on 2018/7/18.
 */
@Controller
@RequestMapping("sys/user")
@Tag(name = "用户 [t_sys_user] 管理")
public class UserExcelCtrl {

    @Resource
    private UserService userService;

    @Resource
    private UserService userSecurityService;

    @Resource
    private ExcelTemplateService excelTemplateService;

    /**
     * 获取Excel模版
     *
     * @param request  -
     * @param response -
     * @throws IOException -
     */
    @Operation(description = "下载用户导入模版")
    @RequestMapping(value = "excel/template", method = RequestMethod.GET)
    public void tplExcel(HttpServletRequest request, HttpServletResponse response) throws IOException {
        try (InputStream is = excelTemplateService.openInputStream("user")) {
            ResponseWriter.download(response, is, "用户导入模版.xls");
        }
    }

    /**
     * 用户信息导入
     */
    @ResponseBody
    @Operation(description = "Excel导入用户")
    @RequestMapping(value = "excel/import", method = RequestMethod.POST)
    public Result importExcel(MultipartFile file) throws IOException {
        List<Object[]> list;
        try (ExcelBook book = ExcelBook.create(file.getInputStream())) {
            list = book.sheetAt(0).setRowReaderAndWriter(ExcelCallback.objectArrayCallback()).read();
        }
        List<User> userList = new ArrayList<>();
        for (Object[] arr : list) {
            User user = new User();
            user.setAccount(Converter.toString(arr[0]));
            user.setPassword(Converter.toString(arr[1]));
            user.setDeptId(Converter.toString(arr[2]));
            userList.add(user);
        }
        return userSecurityService.importUser(userList);
    }


    /**
     * 用户信息导出
     *
     * @param request -
     * @param params  参数
     */
    @Operation(description = "Excel导出用户")
    @RequestMapping(value = "excel/export", method = RequestMethod.POST)
    public void exportExcel(
            HttpServletResponse response, HttpServletRequest request,
            Map<String, Object> params) throws IOException {
        Pagination.startPage(request);
        List<User> l = userService.queryList(params);
        PageInfo<User> pageInfo = new PageInfo<>(l);
        List<User> list = pageInfo.getList();
        try (ExcelBook book = ExcelBook.create()) {
            book.sheetAt(0).setRowReaderAndWriter(ExcelCallback.objectCallback(User.class, new String[]{})).write(list).out(response.getOutputStream());
        }
    }
}
